package com.mvc.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import cn.hutool.core.util.PageUtil;
import com.mvc.gulimall.product.entity.AttrEntity;
import com.mvc.gulimall.product.service.AttrAttrgroupRelationService;
import com.mvc.gulimall.product.service.AttrService;
import com.mvc.gulimall.product.service.CategoryService;
import com.mvc.gulimall.product.vo.AttrGrouoWitAttrVo;
import com.mvc.gulimall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mvc.gulimall.product.entity.AttrGroupEntity;
import com.mvc.gulimall.product.service.AttrGroupService;
import com.mvc.common.utils.PageUtils;
import com.mvc.common.utils.R;
import springfox.documentation.service.ApiListing;


/**
 * 属性分组
 *
 * @author xiao
 * @email 3153579060
 * @date 2022-08-11 17:01:49
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private  AttrService attrService;
    @Autowired
    private    AttrAttrgroupRelationService relationService;



    /**
     * 新增关联关系
     *  //product/attrgroup/attr/relation
     */
    @PostMapping("/attr/relation" )
    public  R addRelation(@RequestBody List<AttrGroupRelationVo> vos ){
        relationService.savaBatch(vos);
        return R.ok();
    }
    /**
     * /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R  getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        // 查出当前分类下的所有属性分组
        List<AttrGrouoWitAttrVo> vos= attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        //查出每个属性分组的 所有属性
       return R.ok().put("data",vos);
    }

    /**
     * 获取指定分组关联的所有属性
     * @param attrgroupId
     * @return
     */
    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public  R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
       List<AttrEntity> entities= attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
public  R attrNoRelation(@RequestParam Map<String, Object> params,
                         @PathVariable("attrgroupId") Long attrgroupId){
    PageUtils page= attrService.getNoRelationAttr(params,attrgroupId);
    return R.ok().put("page",page);
}


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page =  attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        attrGroup.getCatelogPath();

        Long groupId = attrGroup.getAttrGroupId();
          Long[] path =categoryService.findCatelogPath(groupId);
          attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }
    //     /product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public  R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRltion(vos);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
